import { defineStore } from 'pinia'

export const useUserStore = defineStore('user', {
  state: () => ({
    userInfo: null, // 用户信息
  }),
  
  getters: {
    isLoggedIn: (state) => !!localStorage.getItem("token"),
    userId: (state) => {
      if (!localStorage.getItem("token")) return null;
      const storedUserInfo = JSON.parse(localStorage.getItem("userInfo") || "{}");
      return storedUserInfo.id;
    }
  },
  
  actions: {
    // 初始化用户信息
    initUserData() {
      const token = localStorage.getItem("token");
      if (token) {
        const userInfo = JSON.parse(localStorage.getItem("userInfo") || "{}");
        this.userInfo = userInfo;
      }
    },
    
    // 重置状态
    resetStore() {
      this.userInfo = null;
    }
  }
}) 